﻿using System;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.KCS
{
    public partial class SanPhamKCS : UserControl
    {

        public SanPhamKCS()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
            SetPropertiesColumns();
        }

        public void LoadData()
        {
            DataTable dtChoDuyet = DataAll.SelectAll("GiaCong inner join LenhSanXuat on GiaCong.SoChungTu = LenhSanXuat.SoChungTu inner join PhongMau_SanPham on GiaCong.IdSanPham = PhongMau_SanPham.IdSanPham",
                "*,LenhSanXuat.SoLuong as 'SoLuongMau',GiaCong.SoLuong as 'SoLuongThuc',GiaCong.TrangThai as 'TrangThai1'", "WHERE GiaCong.TrangThai = " + Convert.ToInt32(TrangThai.GiaCong_GuiDi) + " OR GiaCong.TrangThai = " + Convert.ToInt32(TrangThai.MayKhoan_GuiDi));
            gridChoDuyet.DataSource = dtChoDuyet;
            DataTable dtDaDuyet = DataAll.SelectAll("KCS inner join LenhSanXuat on KCS.SoChungTu = LenhSanXuat.SoChungTu inner join PhongMau_SanPham on KCS.IdSanPham = PhongMau_SanPham.IdSanPham",
                "*,LenhSanXuat.SoLuong as 'SoLuongMau',KCS.SoLuong as 'SoLuongThuc',KCS.TrangThai as 'TrangThai1'", "");
            gridDaDuyet.DataSource = dtDaDuyet;

        }

        public void SetPropertiesGrid()
        {

            gvDaDuyet.BestFitColumns();
            gvDaDuyet.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvDaDuyet.OptionsBehavior.Editable = false;
            //gvMayKhoan.OptionsSelection.EnableAppearanceFocusedCell = true;
            gvDaDuyet.OptionsSelection.MultiSelect = true;
            gvDaDuyet.OptionsView.ShowGroupPanel = false;
            //gvMayKhoan.OptionsView.ColumnAutoWidth = false;
            gvDaDuyet.OptionsView.ShowFooter = true;
            gvDaDuyet.OptionsFind.AlwaysVisible = true;
            gvDaDuyet.Columns["ID"].Visible = false;

            gvChoDuyet.BestFitColumns();
            gvChoDuyet.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvChoDuyet.OptionsBehavior.Editable = false;
            //gvChoDuyet.OptionsSelection.MultiSelect = true;
            gvChoDuyet.OptionsView.ShowGroupPanel = false;
            gvChoDuyet.OptionsView.ShowFooter = true;
            gvChoDuyet.OptionsFind.AlwaysVisible = true;
            gvChoDuyet.Columns["ID"].Visible = false;

        }

        public void SetPropertiesColumns()
        {
            gcDuyet.Width = 40;
            gcDuyet.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcKhongDuyet.Width = 60;
            gcKhongDuyet.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcMaSanPham.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            //gcNguoiLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLuong.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgaySuaLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTrangThai.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayHoachToan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

        }

        private void btDuyet_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvChoDuyet.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn duyệt.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                int trangthai = Convert.ToInt32(dataview[gvChoDuyet.FocusedRowHandle]["TrangThai"]) == Convert.ToInt32(TrangThai.GiaCong_GuiDi) ? Convert.ToInt32(TrangThai.GiaCong_KCSDaDuyet) : Convert.ToInt32(TrangThai.MayKhoan_KCSDaDuyet);
                string capnhatgiacong = string.Empty;
                capnhatgiacong = DataAll.Update("GiaCong", "TrangThai =" + trangthai + " WHERE ID =" + dataview[gvChoDuyet.FocusedRowHandle]["ID"]);
                var themmoikcs = DataAll.SelectCustomer("INSERT INTO [KCS] VALUES('" + dataview[gvChoDuyet.FocusedRowHandle]["IdSanPham"] + "','"
                         + Convert.ToDateTime(dataview[gvChoDuyet.FocusedRowHandle]["NgayLapLenh"].ToString()).ToString("MM/dd/yyyy") + "','"
                         + Main.UserID + "',"
                         + dataview[gvChoDuyet.FocusedRowHandle]["SoLuong"] + ",'" + DateTime.Now.ToString("MM/dd/yyyy") + "','"
                         + DateTime.Now.ToString("MM/dd/yyyy") + "'," + Convert.ToInt32(TrangThai.KCS_DaDuyet) + ",'"
                         + Convert.ToDateTime(dataview[gvChoDuyet.FocusedRowHandle]["NgayChungTu"].ToString()).ToString("MM/dd/yyyy") + "','"
                         + Convert.ToDateTime(dataview[gvChoDuyet.FocusedRowHandle]["NgayHoachToan"].ToString()).ToString("MM/dd/yyyy") + "','"
                         + dataview[gvChoDuyet.FocusedRowHandle]["SoChungTu"] + "','"
                         + dataview[gvChoDuyet.FocusedRowHandle]["GhiChu"] + "',N'"
                         + "Phòng gia công/may khoán" + "',N'"
                         + "Phòng hoàn thiện" + "','"
                         + Convert.ToDateTime(dataview[gvChoDuyet.FocusedRowHandle]["SXTuNgay"].ToString()).ToString("MM/dd/yyyy") + "','"
                         + Convert.ToDateTime(dataview[gvChoDuyet.FocusedRowHandle]["SXDenNgay"].ToString()).ToString("MM/dd/yyyy") + "') SELECT MAX(ID) FROM KCS");

                MessageBox.Show(capnhatgiacong.Equals("success") && themmoikcs.Rows.Count > 0 ? "KCS duyệt thành công" : capnhatgiacong, "Message",
                                    MessageBoxButtons.OK,
                                    capnhatgiacong.Equals("success") && themmoikcs.Rows.Count > 0 ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                DataAll.InsertAll("HanhTrinh", dataview[gvChoDuyet.FocusedRowHandle]["MaHang"]
                        + "," + Convert.ToInt32(PhongBan.PhongKCS)
                        + "," + dataview[gvChoDuyet.FocusedRowHandle]["SoChungTu"]
                        + "," + Convert.ToInt32(TrangThai.KCS_DaDuyet)
                        + "," + Main.UserID
                        + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                        + "," + dataview[gvChoDuyet.FocusedRowHandle]["SoLuong"]
                        + "," + Convert.ToDateTime(dataview[gvChoDuyet.FocusedRowHandle]["TuNgay"].ToString()).ToString("MM/dd/yyyy")
                        + "," + Convert.ToDateTime(dataview[gvChoDuyet.FocusedRowHandle]["DenNgay"].ToString()).ToString("MM/dd/yyyy"));
                LoadData();
            }
        }

        private void btKhongDuyet_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvChoDuyet.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi trả.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                int trangthai = Convert.ToInt32(dataview[gvChoDuyet.FocusedRowHandle]["TrangThai"]) ==
                                Convert.ToInt32(TrangThai.GiaCong_GuiDi)
                                    ? Convert.ToInt32(TrangThai.Cat_GuiGiaCong)
                                    : Convert.ToInt32(TrangThai.Cat_GuiMayKhoan);
                var capnhatcat = DataAll.Update("Cat", "TrangThai =" + trangthai + " WHERE SoChungTu ='" + dataview[gvChoDuyet.FocusedRowHandle]["SoChungTu"] + "'");
                if (capnhatcat.Equals("success"))
                {
                    DataAll.Delete("GiaCong", "ID =" + dataview[gvChoDuyet.FocusedRowHandle]["ID"]);
                }
                MessageBox.Show(capnhatcat.Equals("success") ? "Gửi trả thành công" : capnhatcat, "Message",
                                       MessageBoxButtons.OK,
                                       capnhatcat.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }

        private void gvChoDuyet_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai1")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void gvDaDuyet_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai1")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void gvChoDuyet_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }

        private void gvChoDuyet_DoubleClick(object sender, EventArgs e)
        {
            if (gvChoDuyet.FocusedRowHandle >= 0)
            {
                SanPhamKCS_ChiTiet.IdMayKhoan = gvChoDuyet.Columns["ID"].View.GetRowCellDisplayText(gvChoDuyet.FocusedRowHandle, "ID");
                var lss = new SanPhamKCS_ChiTiet();
                lss.ShowDialog();
                LoadData();
                SanPhamKCS_ChiTiet.IdMayKhoan = "0";
            }
        }

        private void gvDaDuyet_DoubleClick(object sender, EventArgs e)
        {
            //if (gvDaDuyet.FocusedRowHandle >= 0)
            //{
            //    SanPhamKCS_ChiTiet.IdMayKhoan = gvChoDuyet.Columns["ID"].View.GetRowCellDisplayText(gvChoDuyet.FocusedRowHandle, "ID");
            //    var lss = new SanPhamKCS_ChiTiet();
            //    lss.ShowDialog();
            //    LoadData();
            //    SanPhamKCS_ChiTiet.IdMayKhoan = "0";
            //}
        }

        private void btnHuy_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvDaDuyet.DataSource;
            int trangThaiKCS = Convert.ToInt32(dataview[gvDaDuyet.FocusedRowHandle]["TrangThai"].ToString());

            if (trangThaiKCS == Convert.ToInt32(TrangThai.KCS_HoanThienDaCapNhat))
            {
                MessageBox.Show("Không thể hủy. Phòng hoàn thiện đã cập nhật.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn hủy duyệt.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    DataTable dtGiaCong = DataAll.SelectAll("GiaCong", "*", "WHERE SoChungTu = '" + dataview[gvDaDuyet.FocusedRowHandle]["SoChungTu"] + "'");
                    int trangthai = Convert.ToInt32(dtGiaCong.Rows[0]["TrangThai"].ToString()) == Convert.ToInt32(TrangThai.GiaCong_KCSDaDuyet) ? Convert.ToInt32(TrangThai.GiaCong_GuiDi) : Convert.ToInt32(TrangThai.MayKhoan_GuiDi);
                    string message = DataAll.Update("GiaCong", "TrangThai = " + trangthai + " WHERE SoChungTu ='" + dataview[gvDaDuyet.FocusedRowHandle]["SoChungTu"] + "'");
                    if (message.Equals("success"))
                    {
                        DataAll.Delete("KCS", "ID =" + dataview[gvDaDuyet.FocusedRowHandle]["ID"]);
                    }
                    MessageBox.Show(message.Equals("success") ? "Hủy thành công" : message, "Message",
                                       MessageBoxButtons.OK,
                                       message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    LoadData();
                }
            }

        }

    }
}
